<template>
	<view class="page-wrapper">
		<div class="title" :style="'height:' + navH + 'px'">
			<div @click="goBack" class="title-r"><image src="../../../static/img/public/left.png" mode="widthFix"></image></div>
			<div class="title-c">
				<div class="title-tab">
					<button :class="nowTitleTab === 0 ? 'active' : ''" @click="changeTab(0)">日常保洁</button>
					<button :class="nowTitleTab === 1 ? 'active' : ''" @click="changeTab(1)">深度保洁</button>
				</div>
			</div>
		</div>
		<div class="content">
			<div class="title-tab-item" v-show="nowTitleTab === 0">
				<swiper class="swiper" v-if="rcList.length !== 0" :indicator-dots="true" :autoplay="true" interval="5000" duration="500">
					<swiper-item v-for="(item, index) in rcBanner" :key="index">
						<div class="swiper-image-wrapper"><image class="swiper-image" :src="domain + item" mode="widthFix"></image></div>
					</swiper-item>
				</swiper>
				<div class="type" v-if="rcList.length !== 0">
					<ul>
						<li v-for="item in rcList" :key="item.goods_id" :class="nowRc === item.goods_id ? 'active' : ''" @click="rcTabHandler(item.goods_id)">
							<h3>
								<span v-show="item.sale_price > item.first_price">首单价格{{ item.first_price }}</span>
								<p v-text="item.goods_name"></p>
							</h3>
							<span>
								<span>￥{{ Math.round((item.sale_price - item.discount_price) * 100) / 100 }}</span>
								<p>/{{ item.total_count }}次</p>
							</span>
						</li>
					</ul>
				</div>
				<div class="type-info" v-if="rcList.length !== 0">
					<image v-for="(item, index) in this.nowRcObj.goods_images.split(',')" :key="index" :src="domain + item" mode="widthFix"></image>
				</div>
				<hEmpty v-else center="暂无产品" img="../../static/img/public/order-empty.png"></hEmpty>
			</div>
			<div class="title-tab-item" v-show="nowTitleTab === 1">
				<swiper class="swiper" v-if="sdList.length !== 0" :indicator-dots="true" :autoplay="true" interval="5000" duration="500">
					<swiper-item v-for="(item, index) in sdBanner" :key="index">
						<div class="swiper-image-wrapper"><image class="swiper-image" :src="domain + item" mode="widthFix"></image></div>
					</swiper-item>
				</swiper>
				<div class="type" v-if="sdList.length !== 0">
					<ul>
						<li v-for="item in sdList" :key="item.goods_id" :class="nowSd === item.goods_id ? 'active' : ''" @click="sdTabHandler(item.goods_id)">
							<h3>
								<span v-show="item.sale_price > item.first_price">首单价格{{ item.first_price }}</span>
								<p v-text="item.goods_name"></p>
							</h3>
							<span>
								<span>￥{{ Math.round((item.sale_price - item.discount_price) * 100) / 100 }}</span>
								<p>/{{ item.total_count }}次</p>
							</span>
						</li>
					</ul>
				</div>
				<div class="type-info" v-if="sdList.length !== 0">
					<image v-for="(item, index) in this.nowSdObj.goods_images.split(',')" :key="index" :src="domain + item" mode="widthFix"></image>
				</div>
				<hEmpty v-else center="暂无产品" img="../../static/img/public/order-empty.png"></hEmpty>
			</div>
		</div>
		<div v-if="(nowTitleTab === 0 && rcList.length !== 0) || (nowTitleTab === 1 && sdList.length !== 0)" class="footer">
			<button @click="goConfirmOrder">立即预约</button>
			<button open-type="share" class="btn-share" bindtap="onShareAppMessage"><image src="../../../static/img/public/share.png" mode="widthFix"></image></button>
		</div>

		<!-- 选择增值服务弹窗 -->
		<drawer @confirm="confirmIncrement" cancle overlayHide title="增值服务" height="60%" ref="selectIncrement">
			<checkbox-group class="select-increment" @change="selectIncrementChange">
				<label v-for="item in product.increment" @click="selectIncrementOk(item)">
					<!-- <checkbox :value="item.id" color="#0973F7" :checked="increment.checked.findIndex(item2 => item.id === parseInt(item2)) !== -1" :disabled="isMax && (increment.checked.findIndex(item2 => item.id === parseInt(item2)) === -1)" /> -->
					<span :style="increment.checked.findIndex(item2 => item.id === parseInt(item2)) !== -1 ? 'color: #333333' : 'color: #999999'">{{ item.name }}</span>
					<p :style="increment.checked.findIndex(item2 => item.id === parseInt(item2)) !== -1 ? 'color: #333333' : 'color: #999999'">{{ item.title }}</p>
					<span :style="increment.checked.findIndex(item2 => item.id === parseInt(item2)) !== -1 ? 'color: #333333' : 'color: #999999'">￥{{ item.price }}</span>
				</label>
			</checkbox-group>
		</drawer>
	</view>
</template>

<script>
import drawer from '../../../components/drawer/drawer.vue';
import { api, domain } from '@/util/api.js';
export default {
	data() {
		return {
			navH: 0,
			domain: '',
			nowTitleTab: 0,
			product: {},
			rcList: [],
			nowRc: 0,
			nowRcObj: {},
			rcBanner: [],

			sdList: [],
			nowSd: 0,
			nowSdObj: {},
			sdInfoList: [],
			sdBanner: [],
			increment: {
				checkNum: 1,
				list: [],
				checked: [],
				checkedList: [],
				total_price: 0
			}
		};
	},
	methods: {
		changeTab(type) {
			this.nowTitleTab = type;
			this.getBanner(type);
			this.getProducts(type);
		},
		getBanner(type) {
			switch (type) {
				case 0:
					if (this.rcBanner.length === 0) {
						api.getGoodsBanner({ type: 2 }, res => {
							this.rcBanner = res.data.images;
						});
					}

					break;
				case 1:
					if (this.sdBanner.length === 0) {
						api.getGoodsBanner({ type: 1 }, res => {
							this.sdBanner = res.data.images;
						});
					}

					break;
				default:
					break;
			}
		},
		goBack() {
			// uni.navigateBack();
			uni.switchTab({
				url: '../index'
			});
		},
		getProducts(type) {
			switch (type) {
				case 0:
					if (this.rcList.length === 0) {
						api.getGoodsList({ region_id: getApp().globalData.nowRegion.id, goods_type: 2 }, res => {
							res.data.forEach(item => {
								if (item.status === 1) {
									this.rcList.push(item);
								}
							});
							this.rcTabHandler(this.rcList[0].goods_id);
						});
					}
					break;
				case 1:
					if (this.sdList.length === 0) {
						api.getGoodsList({ region_id: getApp().globalData.nowRegion.id, goods_type: 1 }, res => {
							res.data.forEach(item => {
								if (item.status === 1) {
									this.sdList.push(item);
								}
							});
							this.sdTabHandler(this.sdList[0].goods_id);
						});
					}
					break;
				default:
					break;
			}
		},

		rcTabHandler(goods_id) {
			this.nowRc = goods_id;
			let i = this.rcList.findIndex(item => {
				return item.goods_id === goods_id;
			});
			this.nowRcObj = this.rcList[i];
		},

		sdTabHandler(goods_id) {
			this.nowSd = goods_id;
			let i = this.sdList.findIndex(item => {
				return item.goods_id === goods_id;
			});
			this.nowSdObj = this.sdList[i];
		},

		goConfirmOrder() {
			var that = this;
			if (getApp().globalData.loginStatus === 1) {
				switch (this.nowTitleTab) {
					case 0: //日常保洁
						that.getGoodsInfo(0);
						break;
					case 1:
						that.getGoodsInfo(1);

						break;
					default:
						break;
				}
			} else {
				uni.navigateTo({
					url: '../../Login/Login'
				});
			}
		},

		getGoodsInfo(type) {
			api.getGoodsInfo({ goods_id: type == 0 ? this.nowRcObj.goods_id : this.nowSdObj.goods_id }, res => {
				this.product = res.data;
				console.log('看看', res);
				if (res.data.increment.length == 0) {
					//如果没有规格正常走
					if (type == 0) {
						return uni.navigateTo({
							url: '../confirmOrder/confirmOrder?type=2&&goods_id=' + res.data.goods_id
						});
					} else {
						return uni.navigateTo({
							url: '../confirmOrder/confirmOrder?type=1&&goods_id=' + res.data.goods_id
						});
					}
				}
				this.selectIncrement();
			});
		},

		selectIncrement() {
			this.$refs.selectIncrement.open();
			let obj = { ...this.increment };
			obj.checked = [];
			obj.checkedList.forEach(item => {
				obj.checked.push(item.id);
			});
			this.increment = obj;
		},
		selectIncrementOk(item) {
			console.log('1111', item);
			let a = JSON.stringify(item);

			if (this.nowTitleTab == 0) {
				uni.navigateTo({
					url: '../confirmOrder/confirmOrder?type=2&&goods_id=' + this.nowRcObj.goods_id + '&&increment=' + a
				});
			} else {
				uni.navigateTo({
					url: '../confirmOrder/confirmOrder?type=1&&goods_id=' + this.nowSdObj.goods_id + '&&increment=' + a
				});
			}
		},

		selectIncrementChange(res) {
			let obj = { ...this.increment };
			obj.checked = res.detail.value;
			this.increment = obj;
		}
	},
	onLoad() {
		let app = getApp();
		this.navH = app.globalData.navHeight;
		this.domain = domain;

		uni.showLoading({
			mask: 'true'
		});

		if (app.globalData.loginStatus !== 1) {
			app.login().then(res => {
				this.changeTab(0);
				uni.hideLoading();
			});
		} else {
			this.changeTab(0);
			uni.hideLoading();
		}
	},

	// 分享
	onShareAppMessage: function(res) {
		return {
			title: '嘉洁到家',
			path: '/pages/index/index',
			success: function(res) {
				// 转发成功
				console.log('分享成功');
			},
			fail: function(res) {
				// 转发失败
				console.log('分享失败');
			}
		};
	}
};
</script>

<style scoped>
.page-wrapper {
	box-sizing: border-box;
	display: flex;
	flex-direction: column;
	align-items: center;
	width: 100%;
	/* overflow: visible; */
}
.title {
	display: flex;
	align-items: flex-end;
	width: 100%;
	flex-shrink: 0;
	box-sizing: border-box;
	padding: 0 20rpx;
}
.title > .title-r {
	width: 19rpx;
	height: 66rpx;
	flex-shrink: 0;
	display: flex;
	align-items: center;
}
.title > .title-r > image {
	width: 100%;
}
.title-c {
	flex-grow: 1;
	display: flex;
	justify-content: center;
	align-items: center;
}
.title-tab {
	background-color: #0086fb;
	border: 2rpx solid #0086fb;
	width: 315rpx;
	height: 64rpx;
	font-size: 0;
	border-radius: 32rpx;
	position: relative;
	display: flex;
	align-items: center;
}
.title-tab > button {
	padding: 0;
	height: 64rpx;
	line-height: 64rpx;
	margin: 0;
	color: #ffffff;
	background-color: transparent;
	font-size: 28rpx;
	text-align: center;
	width: 164rpx;
	border-radius: 32rpx;
	position: absolute;
}
.title-tab > button.active {
	background-color: #ffffff;
	color: #0086fb;
}
.title-tab > button:nth-child(1) {
	left: 0;
}
.title-tab > button:nth-child(2) {
	right: 0;
}

.content {
	width: 100%;
	flex-grow: 1;
	box-sizing: border-box;
	padding-top: 30rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	overflow: hidden;
}
.content > .title-tab-item {
	width: 100%;
	flex-grow: 1;
	overflow: auto;
}

.swiper {
	width: 100%;
	height: 470rpx;
	background-color: #eeeeee;
	/* position: absolute;
		top: 0;
		left: 0;
		z-index: -2; */
}
.swiper > swiper-item {
	width: 100%;
}
.swiper-image-wrapper {
	height: 100%;
	width: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
	overflow: hidden;
}
.swiper-image {
	width: 100%;
}

.type {
	width: 100%;
}

.type > ul {
	width: 100%;
	box-sizing: border-box;
	padding: 15rpx 20rpx;
	overflow: auto;
	display: flex;
}
.type > ul > li {
	min-width: 255rpx;
	height: 141rpx;
	background-color: #ffffff;
	box-sizing: border-box;
	padding: 0 20rpx;
	display: flex;
	flex-direction: column;
	justify-content: center;
	flex-shrink: 0;
	margin-right: 12rpx;
	border-radius: 4rpx;
}
.type > ul > li.active {
	background-color: rgba(241, 247, 253, 0.7);
	border: 2rpx solid rgba(45, 125, 234, 0.7);
}
.type > ul > li > h3 {
	width: 100%;
	display: block;
	position: relative;
}
.type > ul > li > h3 > span {
	position: absolute;
	font-size: 18rpx;
	/* width: 94rpx; */
	box-sizing: border-box;
	padding: 0 10rpx;
	height: 25rpx;
	background: linear-gradient(139deg, #fd7000 0%, #fe5000 100%);
	border-radius: 16rpx 0rpx 16rpx 0rpx;
	color: #ffffff;
	text-align: center;
	line-height: 25rpx;
	bottom: 89%;
	left: 0;
}
.type > ul > li > h3 > p {
	font-size: 28rpx;
	color: #333333;
	width: 100%;
	display: block;
	font-weight: 600;
}

.type > ul > li > span {
	display: flex;
	align-items: center;
	margin-top: 13rpx;
}
.type > ul > li > span > span {
	font-size: 24rpx;
	color: #ff3232;
	font-weight: 900;
}
.type > ul > li > span > p {
	font-size: 22rpx;
	color: #999999;
}
.type-info {
	width: 100%;
	font-size: 0;
}
.type-info > image {
	width: 100%;
}

.footer {
	width: 100%;
	flex-shrink: 0;
	height: 136rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	background-color: #ffffff;
	position: relative;
}
.footer > button {
	background-image: linear-gradient(to right, #0973f7, #0286fa);
	width: 502rpx;
	height: 90rpx;
	border-radius: 45rpx;
	border: none;
	color: #ffffff;
	font-size: 34rpx;
}
.footer > .btn-share {
	position: absolute;
	right: 20rpx;
	width: 60rpx;
	height: auto;
	margin: 0;
	line-height: normal;
	padding: 0;
	background-color: transparent;
	background-image: linear-gradient(to right, transparent, transparent);
	display: flex;
	align-items: center;
	justify-content: center;
}
.footer > .btn-share > image {
	width: 100%;
}

.select-increment {
	width: 100%;
	display: flex;
	flex-direction: column;
	align-items: center;
}
.select-increment > label {
	width: 100%;
	display: flex;
	align-items: center;
	border-top: 1rpx solid #eeeeee;
	box-sizing: border-box;
	padding: 30rpx 30rpx;
}
.select-increment > checkbox {
	flex-shrink: 0;
}
.select-increment > label > span:nth-child(2) {
	margin-left: 30rpx;
}
.select-increment > label > span {
	font-size: 24rpx;
	color: #999999;
	flex-shrink: 0;
}
.select-increment > label > p {
	font-size: 24rpx;
	color: #999999;
	box-sizing: border-box;
	padding: 20rpx;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	flex-grow: 1;
	text-align: center;
}
</style>
